/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */
#include "GPKFile.h"
#include "GPKArchive.h"
//#include "LzmaLib.h"
//#include "LzmaEnc.h"

namespace PQEngine
{
	GPKFile::GPKFile()
		: _owner(0),_readPtr(0),_fileId(-1),_fileSize(0),_eof(true),_ok(false)
	{
	}

	GPKFile::GPKFile(GPKArchive* archive,std::string fileName,int id,int fileSize)
		: _owner(archive),_readPtr(0),_fileName(fileName),_fileId(id),_fileSize(fileSize),_isOpen(false),_eof(true),_ok(false)
	{
		assert(_owner);
		if(_owner){
			_ok=true;
		}
	}

	void GPKFile::open()
	{
		assert(!_isOpen);//must not open
		_eof=false;
		_isOpen=true;
		_readPtr=0;
	}

	void GPKFile::close()
	{
		assert(_isOpen);//must open
		_eof=true;
		_isOpen=false;
		_readPtr=0;
		
	}


	int GPKFile::read(char* buf,int size)
	{
		assert(_isOpen);//must open
		int leftover=size;
		if(_readPtr+size>_fileSize){//less than size
			leftover=_fileSize-_readPtr;
			_eof=true;
		}

		_owner->readFileData(_fileId,_readPtr,buf,leftover);
		_readPtr+=leftover;
		return leftover;
	}


	/*
	void Compress1(std::vector<unsigned char> &outBuf,const std::vector<unsigned char> &inBuf)
	{
	  unsigned propsSize = LZMA_PROPS_SIZE;
	  unsigned destLen = inBuf.size() + inBuf.size() / 3 + 128;
	  outBuf.resize(propsSize + destLen);
	  

	  int res = LzmaCompress(
		&outBuf[LZMA_PROPS_SIZE], &destLen,
		&inBuf[0], inBuf.size(),
		&outBuf[0], &propsSize,
		5, 1 << 20, 3,0, 2, 32, 2);
	  
	  assert(propsSize == LZMA_PROPS_SIZE);
	  assert(res == SZ_OK);
	  
	  outBuf.resize(propsSize + destLen);
	}

	static void Uncompress1(std::vector<unsigned char> &outBuf,const std::vector<unsigned char> &inBuf)
	{
	  outBuf.resize(100);//origin size
	  unsigned dstLen = outBuf.size();
	  unsigned srcLen = inBuf.size() - LZMA_PROPS_SIZE;
	  SRes res = LzmaUncompress(
		&outBuf[0], &dstLen,
		&inBuf[LZMA_PROPS_SIZE], &srcLen,
		&inBuf[0], LZMA_PROPS_SIZE);
	  assert(res == SZ_OK);
	  outBuf.resize(dstLen); // If uncompressed data can be smaller
	}


	SRes OnProgress(void *p, UInt64 inSize, UInt64 outSize)
	{
	  // Update progress bar.
	  return SZ_OK;
	}
	static ICompressProgress g_ProgressCallback = { &OnProgress };

	static void * AllocForLzma(void *p, size_t size) { return malloc(size); }
	static void FreeForLzma(void *p, void *address) { free(address); }
	static ISzAlloc SzAllocForLzma = { &AllocForLzma, &FreeForLzma };

	void Compress2(std::vector<unsigned char> &outBuf,const std::vector<unsigned char> &inBuf)
	{
	  unsigned propsSize = LZMA_PROPS_SIZE;
	  unsigned destLen = inBuf.size() + inBuf.size() / 3 + 128;
	  outBuf.resize(propsSize + destLen);

	  CLzmaEncProps props;
	  LzmaEncProps_Init(&props);
	  props.dictSize = 1 << 16; // 64 KB
	  props.writeEndMark = 0; // 0 or 1

	  int res = LzmaEncode(
		&outBuf[LZMA_PROPS_SIZE], &destLen,
		&inBuf[0], inBuf.size(),
		&props, &outBuf[0], &propsSize, props.writeEndMark,
		&g_ProgressCallback, &SzAllocForLzma, &SzAllocForLzma);
	  assert(res == SZ_OK && propsSize == LZMA_PROPS_SIZE);
	  
	  outBuf.resize(propsSize + destLen);
	}
	*/


}
